<?php

namespace api\models\search;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use api\models\VipAssignment;

/**
 * VipAssignmentSearch represents the model behind the search form about `api\models\VipAssignment`.
 */
class VipAssignmentSearch extends VipAssignment
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['item_name', 'user_id'], 'safe'],
            [['availability_num', 'expired_time', 'created_at'], 'integer'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = VipAssignment::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'sort'  => [
                'defaultOrder' => [
                    'created_at' => SORT_DESC,
                ]
            ]
        ]);

        $this->load($params);

        if ( ! $this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'availability_num' => $this->availability_num,
            'expired_time'     => $this->expired_time,
            'created_at'       => $this->created_at,
        ]);

        $query->andFilterWhere(['like', 'item_name', $this->item_name])
              ->andFilterWhere(['like', 'user_id', $this->user_id]);

        return $dataProvider;
    }
}
